package cn.edu.sgu.www.mhxysy.service.qiling.impl;

import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.except.Property;
import cn.edu.sgu.www.mhxysy.entity.qiling.Qiling;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingCategory;
import cn.edu.sgu.www.mhxysy.enums.PropertyTypes;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.mapper.except.PropertyMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingViewAttributeService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.qiling.QilingViewAttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class QilingViewAttributeServiceImpl implements QilingViewAttributeService {

    private final QilingMapper qilingMapper;
    private final PropertyMapper propertyMapper;
    private final EquipmentTypeMapper equipmentTypeMapper;
    private final QilingCategoryMapper qilingCategoryMapper;
    private final EquipmentCategoryMapper equipmentCategoryMapper;
    private final QilingViewAttributeMapper qilingViewAttributeMapper;

    @Autowired
    public QilingViewAttributeServiceImpl(
            QilingMapper qilingMapper,
            PropertyMapper propertyMapper,
            EquipmentTypeMapper equipmentTypeMapper,
            QilingCategoryMapper qilingCategoryMapper,
            EquipmentCategoryMapper equipmentCategoryMapper,
            QilingViewAttributeMapper qilingViewAttributeMapper) {
        this.qilingMapper = qilingMapper;
        this.propertyMapper = propertyMapper;
        this.equipmentTypeMapper = equipmentTypeMapper;
        this.qilingCategoryMapper = qilingCategoryMapper;
        this.equipmentCategoryMapper = equipmentCategoryMapper;
        this.qilingViewAttributeMapper = qilingViewAttributeMapper;
    }

    @Override
    public List<Property> selectProperty(String qilingId) {
        if (StringUtils.isEmpty(qilingId)) {
            QueryWrapper<Property> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("type", PropertyTypes.PTSX.getValue());
            queryWrapper.or();
            queryWrapper.eq("type", PropertyTypes.GJSX.getValue());

            return propertyMapper.selectList(queryWrapper);
        }

        Qiling qiling = qilingMapper.selectById(qilingId);
        QilingCategory qilingCategory = qilingCategoryMapper.selectById(qiling.getCategoryId());
        Integer equipmentId = equipmentTypeMapper.selectTopType(qilingCategory.getEquipmentId());

        return qilingViewAttributeMapper.selectProperty(equipmentId);
    }

    @Override
    public List<QilingViewAttributeVO> selectAttributes(Integer equipmentId) {
        // 防止后面代码NPE
        if (equipmentId == null) {
            return new ArrayList<>();
        }

        EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectById(equipmentId);

        equipmentId = equipmentTypeMapper.selectTopType(equipmentCategory.getId());

        return qilingViewAttributeMapper.selectAttributes(equipmentId, equipmentCategory.getGrade());
    }

}